import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import createUserSlice from './modules/user';

import type { UserSliceType } from './types/index.type';

// 给 store 添加 TS 类型时时，我们要在传入泛型的后面加一个()
// https://github.com/pmndrs/zustand/blob/main/docs/guides/typescript.md
const useStore = create<UserSliceType>()(
  persist(
    (...rest) => ({
      ...createUserSlice(...rest), // 用户模块
    }),
    {
      // 必须传配置项，name代表名称一定要唯一 存储的key
      name: 'PangHu',
      // 默认localStorage, 可以是sessionStorage，IndexDB或移动端asyncStorage
      storage: createJSONStorage(() => localStorage),
    }
  )
);

export default useStore;
